/* -*- mode: c -*-
 * Copyright (C) 2010, 2011 Lluís Vilanova <vilanova@ac.upc.edu>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

//#define _GNU_SOURCE

#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>


#ifndef TF_MM_CONS_INTERNAL_H
#error This file is not intended to be used by applications
#endif

#include "tf_cmd.h"

/* See if the application spends too much time busywaiting */
#define DEBUG_TRIES !NDEBUG
/* Amount of tries to spit out a message on busywaiting */
#define DEBUG_TRIES_COUNT 10
/* Whether each try should be separated by a pseudo-random sleep */
#define TRIES_SLEEP 1





extern long int random(void);

#ifdef __cplusplus
extern "C" {
#endif

#if DEBUG_TRIES
#include <stdio.h>
#endif

/** Pseudo-random exponential backoff. */
inline static
void
_try_sleep (const int tries)
{
#if TRIES_SLEEP
    //const int exp = 2 + (tries / 10);
    // TODO : mark this important modification.
    static const struct timespec notlong  = { 0, 10 * 1000 * 1000 };

    nanosleep( &notlong, NULL );
    //sleep(( random() * exp ) / RAND_MAX);
#endif
}

#if DEBUG_TRIES
#define _TRY_LOOP(cond)                                                 \
    do {                                                                \
        unsigned int tries = 0;                                         \
        while (cond) {                                                  \
            tries++;                                                    \
            if (tries % 100 == 0) {                                     \
                fprintf(stderr, "%s :: too many tries (%u), this might be a bug\n", \
                        __func__, tries);                               \
                fflush(stderr);                                         \
            }                                                           \
            _try_sleep(tries);                                          \
        }                                                               \
    } while (0)
#else  /* DEBUG_TRIES */
#define _TRY_LOOP(cond)                                                 \
    do {                                                                \
        unsigned int tries = 0;                                         \
        while (cond) {                                                  \
            tries++;                                                    \
            _try_sleep(tries);                                          \
        }                                                               \
    } while (0)
#endif  /* DEBUG_TRIES */



inline static
void *
tf_mm_cons_try_shared_acquire (void * base, size_t bytes)
{
    return (void*) TF_CMD_2(TF_MM_CONS_SHARED_ACQUIRE, base, bytes);
}

inline static
void
tf_mm_cons_shared_acquire (void * base, size_t bytes)
{
    _TRY_LOOP(tf_mm_cons_try_shared_acquire(base, bytes) != NULL);
}



inline static
unsigned int
tf_mm_cons_try_upgrade_acquire (void * base)
{
    return TF_CMD_1(TF_MM_CONS_UPGRADE_ACQUIRE, base);
}

inline static
void
tf_mm_cons_upgrade_acquire (void * base)
{
    _TRY_LOOP(tf_mm_cons_try_upgrade_acquire(base) == 0);
}



inline static
void *
tf_mm_cons_try_exclusive_acquire (void * base, size_t bytes)
{
    return (void*) TF_CMD_2(TF_MM_CONS_EXCLUSIVE_ACQUIRE, base, bytes);
}

inline static
void
tf_mm_cons_exclusive_acquire (void * base, size_t bytes)
{
    _TRY_LOOP(tf_mm_cons_try_exclusive_acquire(base, bytes) != NULL);
}



inline static
unsigned int
tf_mm_cons_release (void * base)
{
    uint64_t res = TF_CMD_1(TF_MM_CONS_RELEASE, base);
    return res != 0;
}

#ifdef __cplusplus
}
#endif
